package com.bw.lib_log.logger

import android.util.Log
import com.bw.lib_log.LoggerLevel
import com.bw.lib_log.file.LogFileManager
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

/**
 * 向磁盘中存储日志信息
 */
class FileLogger : ILogger{
    private var isDebug = true
    private var TAG = ""
    private var level = LoggerLevel.DEBUG
    private var path = ""
    private var loggerFormat = "===>$TAG %s ---> %s"//格式化 %s 占位符

    private var fileManager : LogFileManager?= null

    private fun getCurrentTime(): String {
        return SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault()).format(Date())
    }
    override fun v(tag: String, log: String) {
        if (isDebug && level.value >= LoggerLevel.VERBOSE.value){
//            Log.v(TAG,"写到文件中"+String.format(loggerFormat,tag,log))
            fileManager?.writeLog(TAG+":"+getCurrentTime()+":"+String.format(loggerFormat,tag,log))
        }
    }

    override fun d(tag: String, log: String) {
        if (isDebug && level.value >= LoggerLevel.DEBUG.value){
//            Log.v(TAG,"写到文件中"+String.format(loggerFormat,tag,log))
            fileManager?.writeLog(TAG+":"+getCurrentTime()+":"+String.format(loggerFormat,tag,log))
        }
    }

    override fun i(tag: String, log: String) {
        if (isDebug && level.value >= LoggerLevel.INFO.value){
//            Log.v(TAG,"写到文件中"+String.format(loggerFormat,tag,log))
            fileManager?.writeLog(TAG+":"+getCurrentTime()+":"+String.format(loggerFormat,tag,log))
        }
    }

    override fun w(tag: String, log: String) {
        if (isDebug && level.value >= LoggerLevel.WARN.value){
//            Log.v(TAG,"写到文件中"+String.format(loggerFormat,tag,log))
            fileManager?.writeLog(TAG+":"+getCurrentTime()+":"+String.format(loggerFormat,tag,log))
        }
    }

    override fun e(tag: String, log: String) {
        if (isDebug && level.value >= LoggerLevel.ERROR.value){
//            Log.v(TAG,"写到文件中"+String.format(loggerFormat,tag,log))
            fileManager?.writeLog(TAG+":"+getCurrentTime()+":"+String.format(loggerFormat,tag,log))
        }
    }

    override fun setDebug(isDebug: Boolean) {
        this.isDebug = isDebug
    }

    override fun setLevel(level: LoggerLevel) {
        this.level = level
    }

    override fun setTag(tag: String) {
        this.TAG = tag
    }

    override fun setSavaUrl(path: String) {
        this.path = path
            fileManager = LogFileManager(File(path))
    }
}